<?php
namespace app\admin\service;
use think\Db;
class  Adport{
    //登录
    function registerApi($username,$password,$verification){
        if(empty($username) || empty($password) || empty($verification)){
            return messageInfo('1001','参数不正确');
        }
        if(!captcha_check($verification)){
            return  messageInfo('1001','验证码错误');
        }
        $admin = Db::name('admin');
        $where['a_username'] =$username;//登录账号
        $info = $admin->where($where)->find();
        if(empty($info)){
            return messageInfo('1001','用户不存在');
        }
        if($info['a_password'] != md5($password)){
            return messageInfo('1001','密码错误');
        }
        //查询用户角色所有功能权限
        $sql = "select * from lt_admin_function where admin_function_id in(select admin_function_id from lt_admin_role_function where admin_role_id =".$info['a_role'].")";
        $rolist_qp = Db::query($sql);
        if(empty($rolist_qp)){
            return messageInfo('1001','未分配权限');
        }
        $rolist_qer = Db::name('admin_function')->where('af_strte',2)->select();
        $rolist     = array_merge_recursive($rolist_qp,$rolist_qer);
        $a = Db::table('lt_admin')->where($where)->setField('a_gotime',Date('Y-m-d H:i:s'));
        if(!$a){
            return messageInfo('1001','登录失败');
        }
        session('Admin_user_lvtuan',$info);
        session('wyrolelist_lvtuan', $rolist);  //设置session
        session('LVTUAN_NAME', $info['a_fullname']);  //设置session
        $url      = config('tempredirect_url').'admin/main';
        $response = messageInfo('1002','登录成功','',$url);
        return $response;
    }


    //后台左边栏
    function main_list(){
        $menudata = Db::name('Admin_menu')->order('am_sort asc')->select();
        $menuinfo = array();
        $count    = 0;
        foreach ($menudata as $key => $value) {
            if ($value['am_pid'] == 0) {
                $count1 = 0;
                $child = array();
                foreach ($menudata as $key1 => $value1) {
                    if ($value1['am_pid'] == $value['admin_menu_id']) {
                        if (CheckAuthority($value1['am_murl'])) {
                            $child[$count1] = $value1;
                            $count1++;
                        }
                    }
                }
                if (count($child) > 0) {
                    $menuinfo[$count]['am_mname'] = $value['am_mname'];
                    $menuinfo[$count]['am_murl'] = $value['am_murl'];
                    $menuinfo[$count]['child'] = $child;
                    $count++;
                }
            }
        }
        return $menuinfo;
    }



}
